{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1258764c-b950-49d5-b098-4c86468f50f6",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "# 嵌套列表浅复制 vs 深复制\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0aa31dd-78cd-4498-8cf7-9f2c478c585b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始list\n",
      "[1, 2, 3, [4, 5]]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "\n",
    "list1 = [1, 2, 3, [4, 5]]\n",
    "print('原始list')\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e864b824-c851-4271-bcb0-a8cc3c56078d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 深复制，适用于嵌套列表\n",
    "list_deep = copy.deepcopy(list1)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "deb8635b-5af3-48b2-81a5-29e94afa5488",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只深复制一层\n",
    "list2 = list1.copy() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73a7328d-5f1c-401d-9d51-a400810be138",
   "metadata": {},
   "outputs": [],
   "source": [
    "list3 = list1[:]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cbc8d968-4429-4966-bb94-68e676ccac46",
   "metadata": {},
   "outputs": [],
   "source": [
    "list4 = list(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "213daf61-fe33-4187-90ea-aec54fca25da",
   "metadata": {},
   "outputs": [],
   "source": [
    "list5 = [*list1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b3cf65c4-209d-41b9-ba14-0dffa6eb0bb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改元素\n",
    "list_deep[3][0] = 'deep'\n",
    "list_deep[2] = 'worked_0'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d54058e8-4f77-4bfe-bff6-08110fbc7fd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "list2[3][0] = 'abc'\n",
    "list2[2] = 'worked_1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a06a1880-1f53-4fa8-9402-6bf46fe02342",
   "metadata": {},
   "outputs": [],
   "source": [
    "list3[3][0] = 'X1'\n",
    "list3[2] = 'worked_2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9f3e38c4-9419-4acc-a0ca-871a5118a9a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "list4[3][0] = 'X2'\n",
    "list4[2] = 'worked_3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "30764a4d-83b3-4cfe-ba89-b19b34e74eac",
   "metadata": {},
   "outputs": [],
   "source": [
    "list5[3][0] = 'X3'\n",
    "list5[2] = 'worked_4'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c1023de6-bb61-4097-a3d7-c468122326b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新list\n",
      "[1, 2, 3, ['X3', 5]]\n",
      "[1, 2, 'worked_0', ['deep', 5]]\n"
     ]
    }
   ],
   "source": [
    "print('新list')\n",
    "print(list1) \n",
    "print(list_deep)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cd5bca98-fb92-4dbc-b271-8182f13aaa90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'worked_1', ['X3', 5]]\n",
      "[1, 2, 'worked_2', ['X3', 5]]\n",
      "[1, 2, 'worked_3', ['X3', 5]]\n",
      "[1, 2, 'worked_4', ['X3', 5]]\n"
     ]
    }
   ],
   "source": [
    "print(list2)  \n",
    "print(list3)\n",
    "print(list4)\n",
    "print(list5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e1a0cd4-da12-42f2-aeec-f28e09b15637",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
